var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    ifShownoData: true,
    //
    init: false,
    typeArrSr: [
      // 1:資源回收, 2:廚餘減量, 3:邀請好友, 4:購物兌換, 5:簽到
      {
        name: '销货收入',
        id: 1
      },
      {
        name: '杂料收入',
        id: 2
      },
      {
        name: '租金收入',
        id: 3
      },
      {
        name: '其他收入',
        id: 4
      }
    ],
    typeArrZc: [
      {
        name: '购货支出',
        id: 5
      },
      {
        name: '工资支出',
        id: 6
      },
      {
        name: '运输支出',
        id: 7
      },
      {
        name: '租金水电',
        id: 8
      },
      {
        name: '设备及维修',
        id: 9
      },
      {
        name: '其他支出',
        id: 10
      }
    ],
    typeId: 0,
    currentPage: 1,
    totalPage: 1,
    typeTop: 300, //初始化 隐藏高度
    searchArr: [],
    typeClick: false,
    dataClick: false,
    slideDown: false,
    dataType: 1, //1:按月  2 按日
    //按月
    MonthCurrentDateStr: new Date().getTime(), //当前时间戳
    MonthCurrentDate: '', //转化后的当前时间2018-1
    maxDate: new Date().getTime(),
    //按日
    FirstT: '1', //切换日期选择 起止日和终止日
    //起始
    DayFcurrentDateStr: new Date().getTime(), //起始日
    DayFcurrentDate: '',
    DayFmaxDate: new Date().getTime(),
    //终止
    DayLcurrentDateStr: new Date().getTime(), //起始日
    DayLcurrentDate: '',
    DayLmaxDate: new Date().getTime(),
    //
    startDate: '', //搜索起始日期
    endDate: '', //搜索终止日期
    //
    result: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function(options) {
    this.setData({
      init: false,
      typeClick: false,
      dataClick: false,
      slideDown: false,
      currentPage: 1,
      totalPage: 1
    })
    let MonthCurrentDate = this.getMonth(this.data.MonthCurrentDateStr)
    let DayFcurrentDate = this.getDay(this.data.DayFcurrentDateStr)
    let DayLcurrentDate = this.getDay(this.data.DayLcurrentDateStr)
    this.setData({ MonthCurrentDate, DayFcurrentDate, DayLcurrentDate })
    this.searchRecords()
  },
  //到厂数据登记
  toReceiptsBookkeepingAdd: function() {
    wx.navigateTo({
      url: 'receiptsBookkeeping_add'
    })
  },

  chooseFL(e) {
    // console.log(e.currentTarget.dataset.index)
    this.setData({ FirstT: e.currentTarget.dataset.index })
  },
  onMonthChange(event) {
    // console.log(event)
    let val = event.detail.data.innerValue
    let MonthCurrentDate = this.getMonth(val)
    this.setData({
      MonthCurrentDate,
      MonthCurrentDateStr: event.detail.data.innerValue
    })
  },
  onDayFChange(event) {
    // console.log(event.detail.data.innerValue)
    let val = event.detail.data.innerValue
    let DayFcurrentDate = this.getDay(val)
    this.setData({
      DayFcurrentDate,
      DayFcurrentDateStr: event.detail.data.innerValue
    })
  },
  onDayLChange(event) {
    // console.log(event.detail.data.innerValue)
    let val = event.detail.data.innerValue
    let DayLcurrentDate = this.getDay(val)
    this.setData({
      DayLcurrentDate,
      DayLcurrentDateStr: event.detail.data.innerValue
    })
  },
  getMonth(tm) {
    //时间戳转化为年-月
    let myDate = new Date(tm)
    let year = myDate.getFullYear()
    let month = myDate.getMonth() + 1
    return year + '-' + this.add(month)
  },
  getDay(tm) {
    //时间戳转化为年-月-日
    let myDate = new Date(tm)
    let year = myDate.getFullYear()
    let month = myDate.getMonth() + 1
    let day = myDate.getDate()
    return year + '-' + this.add(month) + '-' + this.add(day)
  },
  add(m) {
    return m < 10 ? '0' + m : m
  },
  bindType(e) {
    //分类选择
    this.setData({
      typeId: e.currentTarget.dataset.typeid
    })
  },
  chooseType() {
    this.setData({ init: true })
    // console.log(this.data.typeTop)
    if (!this.data.typeClick) {
      this.setData({ typeClick: true, slideDown: true, dataClick: false })
    } else {
      this.setData({ typeClick: false, slideDown: false, dataClick: false })
    }
  },
  chooseCalendar() {
    this.setData({ init: true })
    if (!this.data.dataClick) {
      this.setData({ dataClick: true, slideDown: true, typeClick: false })
    } else {
      this.setData({ dataClick: false, slideDown: false, typeClick: false })
    }
  },
  chooseDataType() {
    if (this.data.dataType == 1) {
      this.setData({ dataType: 2 }) //按日
    } else {
      this.setData({ dataType: 1 }) //按月
    }
  },
  DayBtn() {
    //按日搜索按扭
    this.setData({
      dataClick: false,
      slideDown: false,
      currentPage: 1,
      totalPage: 1
    })
    //判断时间 大小
    if (this.data.DayLcurrentDateStr > this.data.DayFcurrentDateStr) {
      // console.log('终止日大')
      this.setData({
        startDate: this.data.DayFcurrentDate + ' 00:00:00',
        endDate: this.data.DayLcurrentDate + ' 00:00:00'
      })
    } else {
      // console.log('起止日大')
      this.setData({
        startDate: this.data.DayLcurrentDate + ' 00:00:00',
        endDate: this.data.DayFcurrentDate + ' 00:00:00'
      })
    }
    wx.pageScrollTo({ scrollTop: 0, duration: 0 })
    this.searchRecords()
  },
  MonthBtn() {
    //按月搜索按扭
    this.setData({
      dataClick: false,
      slideDown: false,
      currentPage: 1,
      totalPage: 1
    })
    // console.log(this.data.MonthCurrentDate)
    this.setData({
      startDate: this.data.MonthCurrentDate + '-01' + ' 00:00:00',
      endDate: this.data.MonthCurrentDate + '-31' + ' 00:00:00'
    })
    wx.pageScrollTo({ scrollTop: 0, duration: 0 })
    this.searchRecords()
  },
  typeBtn() {
    //分类选择按钮
    this.setData({ typeClick: false, slideDown: false })
    this.setData({ currentPage: 1, totalPage: 1 })
    wx.pageScrollTo({ scrollTop: 0, duration: 0 })
    this.searchRecords()
  },
  bindMask() {
    this.setData({ typeClick: false, dataClick: false, slideDown: false })
  },
  searchRecords() {
    console.log(
      '当前页' + this.data.currentPage + '; 总页数' + this.data.totalPage
    )
    console.log('类型' + this.data.typeId)
    console.log('时间段' + this.data.startDate + '---' + this.data.endDate)
    let that = this
    wx.showLoading({ title: '加载中' })
    wx.request({
      method: 'POST',
      url: app.preUrl + 'getBill',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: app.globalData.userId,
        beginTime: this.data.startDate,
        endTime: this.data.endDate,
        bookSubject: that.data.typeId,
        size: 10,
        page: that.data.currentPage
      },
      success: function(res) {
        wx.hideLoading()
        if (res.data.code == 100) {
          that.setData({ totalPage: res.data.total / 10 })
          let data = res.data.result
          console.log(data)
          if (that.data.currentPage != 1) {
            let array = that.data.result
            that.setData({ result: array.concat(data) })
          } else {
            that.setData({ result: data })
          }
          that.setData({ ifShownoData: true })
        } else if (res.data.code == 400) {
          that.setData({ ifShownoData: false, result: [] })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.message,
            confirmColor: '#56BDE0',
            showCancel: false
          })
        }
      },
      fail: function(error) {}
    })
  },
  onReachBottom: function() {
    let currentPage = this.data.currentPage
    if (currentPage >= this.data.totalPage) {
      console.log('到底了')
      return
    }
    currentPage++
    this.setData({ currentPage })
    this.searchRecords()
  },
  previewImage(e) {
    let arr = e.currentTarget.dataset.imgs
    let index = e.currentTarget.dataset.index
    wx.previewImage({
      current: arr[index], // 当前显示图片的http链接
      urls: arr // 需要预览的图片http链接列表
    })
  },
  del(e) {
    let that = this
    let id = e.currentTarget.dataset.id

    wx.showModal({
      title: '提示',
      content: '确认删除该笔记账信息？',
      cancelText: '取消',
      confirmColor: '#50BEC5',
      success: function(res) {
        if (res.confirm) {
          console.log('确认')
          wx.request({
            method: 'post',
            url: app.preUrl + 'deleteBook',
            dataType: 'json',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
              id: id
            },
            success: function(res) {
              if (res.data.code == 100) {
                wx.showToast({
                  title: '删除成功',
                  icon: 'none',
                  duration: 1000
                })
                that.setData({
                  currentPage: 1,
                  totalPage: 1
                })
                wx.pageScrollTo({ scrollTop: 0, duration: 0 })
                that.searchRecords()
              } else {
                wx.showModal({
                  title: '提示',
                  content: res.data.message,
                  confirmColor: '#56BDE0',
                  showCancel: false
                })
              }
            },
            fail: function(error) {}
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
})
